/*
 * SubmitOrderSuccessFragment.java
 * business
 *
 * Created by chentao on 2017/5/9.
 *
 * Copyright (c) 2017年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.ui.main.shoppingcart.multipayways;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.TextView;

import com.yidiandao.business.R;
import com.yidiandao.business.base.SuperBaseFragment;
import com.yidiandao.business.data.response.QueryMultiPayWaysResultResponse;
import com.yidiandao.business.ui.main.shoppingcart.orderresult.OrderResultActivity;
import com.yidiandao.business.ui.orderabout.order.OrderActivity;
import com.yidiandao.business.ui.widget.recyclerview.provider.MultiPayWayInfoProvider;
import com.yidiandao.business.ui.widget.recyclerview.provider.ToOnlinePayProvider;
import com.yidiandao.business.utils.UIUtils;

import java.util.List;

import me.drakeet.multitype.MultiTypeAdapter;

/**
 * 提交订单成功-View
 */
public class MultiPayWaysFragment extends SuperBaseFragment implements MultiPayWaysContract.View {

    private MultiPayWaysContract.Presenter mPresenter;

    private Toolbar mToolbar;

    private TextView mTvOrderCenter;

    private RecyclerView mRecyclerView;

    private MultiTypeAdapter mAdapter;

    public static MultiPayWaysFragment getInstance() {
        MultiPayWaysFragment fragment = new MultiPayWaysFragment();
        return fragment;
    }

    @Override
    public void setPresenter(MultiPayWaysContract.Presenter presenter) {
        mPresenter = presenter;
    }

    @Override
    public String getPagerTag() {
        return MultiPayWaysFragment.class.getSimpleName();
    }

    @Override
    public void showToast(String text) {
        UIUtils.showToast(text);
    }

    @Override
    protected void initViews() {
        mToolbar = (Toolbar) mRootView.findViewById(R.id.toolbar);
        mTvOrderCenter = (TextView) mRootView.findViewById(R.id.tv_order_center);
        mRecyclerView = (RecyclerView) mRootView.findViewById(R.id.recycler_view);

        initRecyclerView();
        initListeners();
    }

    private void initListeners() {
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mActivity.finish();
                mPresenter.toOrderResultActivity();
            }
        });

        mTvOrderCenter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPresenter.needRefreshData();
                OrderActivity.actionStart(mActivity, 0);
            }
        });
    }

    @Override
    public void toOrderResultActivity(String orderPayId) {
        OrderResultActivity.actionStart(mActivity, orderPayId, null);
    }

    private void initRecyclerView() {
        mAdapter = new MultiTypeAdapter();
        mAdapter.register(QueryMultiPayWaysResultResponse.PayWayState.class, new MultiPayWayInfoProvider());
        mAdapter.register(QueryMultiPayWaysResultResponse.OnlinePayState.class, new ToOnlinePayProvider(mActivity));

        mRecyclerView.setLayoutManager(new LinearLayoutManager(mActivity));
        mRecyclerView.setAdapter(mAdapter);
    }

    @Override
    public void setItems(List<?> items) {
        mAdapter.setItems(items);
        mAdapter.notifyDataSetChanged();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_multi_payways;
    }

    public void onBackPress() {
        mPresenter.toOrderResultActivity();
    }

    @Override
    protected void startLoadData() {
        mPresenter.start();
    }

    @Override
    public void refreshContent(int result) {
        superRefreshLayoutState(result);
    }

    @Override
    public int checkResultObject(Object resultObject) {
        return superCheckListState(resultObject);
    }

    @Override
    public void onResume() {
        super.onResume();
        mPresenter.checkRefreshData();
    }
}
